package com.mjs_svc.lywriter.app;

import java.util.*;
import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;

/**
 * LyWriter.java
 *
 * Created on Dec 18, 2009, 1:56:08 PM
 * @author Matthew Scott
 * @version $Id: LyWriter.java 10 2010-02-16 06:32:51Z mjs@mjs-svc.com $
 */
public class LyWriter extends javax.swing.JFrame {
    private ResourceBundle menuRB = ResourceBundle.getBundle("com/mjs_svc/lywriter/resources/Menus");
    private ResourceBundle errorsRB = ResourceBundle.getBundle("com/mjs_svc/lywriter/resources/Errors");
    private Properties lyWriterProps = new Properties();

    /** 
     * Creates new form LyWriter
     */
    public LyWriter(String[] files) {

        // Initialize components
        initComponents();

        /*
         * Try to load properties.  If the properties file doesn't exist, then
         * run through a small wizard to generate one
         */
        try {
            FileInputStream lyprops = new FileInputStream(new File(System.getProperty("user.home") + File.separator + "lywriter.properties"));
            lyWriterProps.load(lyprops);
        } catch (java.io.FileNotFoundException ex) {
            Logger.getLogger(LyWriter.class.getName()).log(Level.WARNING, null, ex);
            initProperties();
        } catch (IOException ex) {
            Logger.getLogger(LyWriter.class.getName()).log(Level.SEVERE, null, ex);
        }

        if (lyWriterProps.getProperty("lilypond") == null) {
            Logger.getLogger(LyWriter.class.getName()).log(Level.SEVERE, "lilypond property not found");
        }
    }

    /**
     * Generate a new lywriter.properties file containing at least the path to
     * the Lilypond executable.
     */
    private void initProperties() {
        JFileChooser whereIsLy = new JFileChooser();
        int response;

        // Ask if the user really wants to create a new properties file
        response = JOptionPane.showConfirmDialog(
                rootPane,
                errorsRB.getString("init.initproperties") + System.getProperty("user.home") + File.separator,
                errorsRB.getString("init.initproperties.title"),
                JOptionPane.YES_NO_OPTION);

        // If so, continue
        if (response == JOptionPane.YES_OPTION) {

            // open a file chooser
            response = whereIsLy.showOpenDialog(rootPane);

            // if the user didn't cancel, try to write out a properties file
            if (response == JFileChooser.APPROVE_OPTION) {
                String lilypath = whereIsLy.getSelectedFile().getPath();
                lyWriterProps.setProperty("lilypond", lilypath);
                lyWriterProps.setProperty("recent1", "");
                lyWriterProps.setProperty("recent2", "");
                lyWriterProps.setProperty("recent3", "");
                lyWriterProps.setProperty("recent4", "");
                lyWriterProps.setProperty("recent5", "");
                try {
                    FileOutputStream lyprops = new FileOutputStream(new File("lywriter.properties"));
                    lyWriterProps.store(lyprops, "lyWriter basic properties");
                } catch (IOException ex) {
                    Logger.getLogger(LyWriter.class.getName()).log(Level.SEVERE, null, ex);
                }
                return;
            }
        }

        // If we've made it here, show an error message and exit.
        JOptionPane.showMessageDialog(
                rootPane,
                errorsRB.getString("init.initproperties.no") + System.getProperty("user.home") + File.separator,
                errorsRB.getString("init.initproperties.no.title"),
                JOptionPane.ERROR_MESSAGE);
        System.exit(0);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        desktopPane = new javax.swing.JDesktopPane();
        menuBar = new javax.swing.JMenuBar();
        fileMenu = new javax.swing.JMenu();
        openMenuItem = new javax.swing.JMenuItem();
        saveMenuItem = new javax.swing.JMenuItem();
        saveAsMenuItem = new javax.swing.JMenuItem();
        exportMenu = new javax.swing.JMenu();
        exportPDFMenuItem = new javax.swing.JMenuItem();
        exportMidiMenuItem = new javax.swing.JMenuItem();
        exitMenuItem = new javax.swing.JMenuItem();
        editMenu = new javax.swing.JMenu();
        cutMenuItem = new javax.swing.JMenuItem();
        copyMenuItem = new javax.swing.JMenuItem();
        pasteMenuItem = new javax.swing.JMenuItem();
        deleteMenuItem = new javax.swing.JMenuItem();
        scoreMenu = new javax.swing.JMenu();
        scorePropertiesMenuItem = new javax.swing.JMenuItem();
        staffMenu = new javax.swing.JMenu();
        staffPropertiesMenuItem = new javax.swing.JMenuItem();
        toolsMenu = new javax.swing.JMenu();
        lyPreviewMenuItem = new javax.swing.JMenuItem();
        helpMenu = new javax.swing.JMenu();
        contentMenuItem = new javax.swing.JMenuItem();
        aboutMenuItem = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        fileMenu.setText(menuRB.getString("file"));

        openMenuItem.setText(menuRB.getString("file.open"));
        fileMenu.add(openMenuItem);

        saveMenuItem.setText(menuRB.getString("file.save"));
        fileMenu.add(saveMenuItem);

        saveAsMenuItem.setText(menuRB.getString("file.saveas"));
        fileMenu.add(saveAsMenuItem);

        exportMenu.setText(menuRB.getString("file.export"));

        exportPDFMenuItem.setText("PDF");
        exportMenu.add(exportPDFMenuItem);

        exportMidiMenuItem.setText("MIDI");
        exportMenu.add(exportMidiMenuItem);

        fileMenu.add(exportMenu);

        exitMenuItem.setText(menuRB.getString("file.exit"));
        exitMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                exitMenuItemActionPerformed(evt);
            }
        });
        fileMenu.add(exitMenuItem);

        menuBar.add(fileMenu);

        editMenu.setText(menuRB.getString("edit"));

        cutMenuItem.setText(menuRB.getString("edit.cut"));
        editMenu.add(cutMenuItem);

        copyMenuItem.setText(menuRB.getString("edit.copy"));
        editMenu.add(copyMenuItem);

        pasteMenuItem.setText(menuRB.getString("edit.paste"));
        editMenu.add(pasteMenuItem);

        deleteMenuItem.setText(menuRB.getString("edit.delete"));
        editMenu.add(deleteMenuItem);

        menuBar.add(editMenu);

        scoreMenu.setText(menuRB.getString("score"));

        scorePropertiesMenuItem.setText(menuRB.getString("score.properties"));
        scoreMenu.add(scorePropertiesMenuItem);

        menuBar.add(scoreMenu);

        staffMenu.setText(menuRB.getString("staff"));

        staffPropertiesMenuItem.setText(menuRB.getString("staff.properties"));
        staffMenu.add(staffPropertiesMenuItem);

        menuBar.add(staffMenu);

        toolsMenu.setText(menuRB.getString("tools"));

        lyPreviewMenuItem.setText(menuRB.getString("tools.preview"));
        toolsMenu.add(lyPreviewMenuItem);

        menuBar.add(toolsMenu);

        helpMenu.setText(menuRB.getString("help"));

        contentMenuItem.setText(menuRB.getString("help.contents"));
        helpMenu.add(contentMenuItem);

        aboutMenuItem.setText(menuRB.getString("help.about"));
        helpMenu.add(aboutMenuItem);

        menuBar.add(helpMenu);

        setJMenuBar(menuBar);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(desktopPane, javax.swing.GroupLayout.DEFAULT_SIZE, 644, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(desktopPane, javax.swing.GroupLayout.DEFAULT_SIZE, 279, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void exitMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exitMenuItemActionPerformed
        System.exit(0);
    }//GEN-LAST:event_exitMenuItemActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(final String[] args) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new LyWriter(args).setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenuItem aboutMenuItem;
    private javax.swing.JMenuItem contentMenuItem;
    private javax.swing.JMenuItem copyMenuItem;
    private javax.swing.JMenuItem cutMenuItem;
    private javax.swing.JMenuItem deleteMenuItem;
    private javax.swing.JDesktopPane desktopPane;
    private javax.swing.JMenu editMenu;
    private javax.swing.JMenuItem exitMenuItem;
    private javax.swing.JMenu exportMenu;
    private javax.swing.JMenuItem exportMidiMenuItem;
    private javax.swing.JMenuItem exportPDFMenuItem;
    private javax.swing.JMenu fileMenu;
    private javax.swing.JMenu helpMenu;
    private javax.swing.JMenuItem lyPreviewMenuItem;
    private javax.swing.JMenuBar menuBar;
    private javax.swing.JMenuItem openMenuItem;
    private javax.swing.JMenuItem pasteMenuItem;
    private javax.swing.JMenuItem saveAsMenuItem;
    private javax.swing.JMenuItem saveMenuItem;
    private javax.swing.JMenu scoreMenu;
    private javax.swing.JMenuItem scorePropertiesMenuItem;
    private javax.swing.JMenu staffMenu;
    private javax.swing.JMenuItem staffPropertiesMenuItem;
    private javax.swing.JMenu toolsMenu;
    // End of variables declaration//GEN-END:variables

}
